package com.test.qpbgboot.base.utils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class BaseUtils {

    public static void removeBlank(Object object) {
        Class<?> cls = object.getClass();
        Field[] fields = cls.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(object);
                if (value instanceof String && ((String) value).trim().isEmpty()) {
                    field.set(object, null);
                }
                field.setAccessible(false);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将LocalDateTime转化成Date
     */
    public static Date toDate(LocalDateTime time) {
        ZoneId zoneId = ZoneId.systemDefault();
        return Date.from(time.atZone(zoneId).toInstant());
    }

    /**
     * 将imgList转化成imgsString
     */
    public static String imgListToString(List<String> imgList) {
        if (imgList != null && !imgList.isEmpty()) {
            return imgList.stream().filter(Objects::nonNull)
                    .collect(Collectors.joining(","));
        }
        return null;
    }

    /**
     * 将imgsString转化成imgList
     */
    public static List<String> imgStringToList(String imgString) {
        if (imgString != null) {
            return Arrays.asList(imgString.split(","));
        } else {
            return null;
        }
    }
}
